package com.jianzh5.action.admin;

import com.jianzh5.common.DataPage;
import com.jianzh5.common.DataTables;
import com.jianzh5.common.ResultJson;
import com.jianzh5.pojo.PCategory;
import com.jianzh5.service.CategoryServiceI;
import com.jianzh5.util.InfoCenterHome;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/product/category")
public class CategoryAction {
	@Autowired
	private CategoryServiceI categoryService;
	
	@RequiresPermissions("product:category:view")
	@RequestMapping("/categoryPage")
	public String categoryPage(){
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/product/categoryList";

	}
	
	@RequestMapping("/categoryList")
	@ResponseBody
	@RequiresPermissions("product:category:view")
	public DataTables categoryList(DataPage datapage, HttpServletRequest request){
		String sindex = request.getParameter("order[0][column]");
		String order = request.getParameter("order[0][dir]");
		String sort = request.getParameter("columns["+sindex+"][data]");
		DataTables dt = categoryService.dataTables(order,sort,datapage);
		return dt;
	}
	
	@RequiresPermissions("product:category:add")
	@RequestMapping("/categoryAddPage")
	public String categoryAddPage(){
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/product/categoryAdd";
	}
	
	@RequiresPermissions("product:category:add")
	@RequestMapping("/categoryAdd")
	@ResponseBody
	public ResultJson categoryAdd(PCategory pcate){
		ResultJson json = new ResultJson();
		try{
			categoryService.addCategory(pcate);
			json.setSuccess(true);
			json.setMsg("产品类别添加成功...");
		}catch (Exception e) {
			json.setMsg("产品类别添加失败,数据库异常...");
		}
		return json;
	}
	
	@RequiresPermissions("product:category:del")
	@RequestMapping("/categoryDel")
	@ResponseBody
	public ResultJson categoryDel(@RequestParam(value="id") int id){
		ResultJson json = new ResultJson();
		try{
			categoryService.delCategory(id);
			json.setSuccess(true);
			json.setMsg("产品类别刪除成功...");
		}catch (Exception e) {
			json.setMsg("产品类别刪除失败,数据库异常...");
		}
		return json;
	}
	
	@RequiresPermissions("product:category:edit")
	@RequestMapping("/editCategoryPage")
	public String editCategoryPage(@RequestParam(value = "id") int id,Model model){
		PCategory pCate = categoryService.getPCategory(id);
		model.addAttribute("pCate", pCate);
		return InfoCenterHome.SERVLET_ADMIN_ROOT+"/product/categoryEdit";
	}
	
	@RequestMapping("/categoryEdit")
	@ResponseBody
	@RequiresPermissions("product:category:edit")
	public ResultJson categoryEdit(PCategory pcate){
		ResultJson json = new ResultJson();
		try{
			categoryService.editCategory(pcate);
			json.setSuccess(true);
			json.setMsg("产品类别编辑成功...");
		}catch (Exception e) {
			json.setMsg("产品类别编辑异常...");
		}
		return json;
	}
	
}
